package main

import (
	"flag"
	"fmt"
	"log"
	"math"
	"math/rand"
	"os"

	"github.com/gocarina/gocsv"
	"github.com/rs/xid"
)

var (
	max *int
)

type robot struct {
	OpenId    string `csv:"openId"`
	Nick      string `csv:"nick"`  // 登录时的标识
	Level     uint8  `csv:"level"` // 等级
	InitValue uint16 `csv:"value"` // 初始值
}

func init() {
	max = flag.Int("m", 1000, "max player")
	flag.Parse()

	log.SetFlags(log.LstdFlags | log.Lshortfile)
}

func main() {
	const fileName = "robots.csv"
	if fs, err := os.Stat(fileName); err == nil && !os.IsNotExist(err) && fs.Mode().IsRegular() {
		if err := os.Remove(fileName); err != nil {
			log.Fatalf("Error removing file: %v", err)
		}
	}

	file, err := os.OpenFile(fileName, os.O_RDWR|os.O_CREATE, os.ModePerm)
	if err != nil {
		log.Fatal(err.Error())
	}
	defer file.Close()

	robots := make([]*robot, 0, *max)
	for i := 1; i <= *max; i++ {
		robots = append(robots, &robot{
			OpenId:    xid.New().String(),
			Nick:      fmt.Sprintf("robot%d", i),
			Level:     uint8(rand.Uint32()%math.MaxInt8) + 1,
			InitValue: uint16(rand.Int31()%math.MaxInt16) + 1,
		})
	}

	if err := gocsv.Marshal(&robots, file); err != nil {
		log.Fatal(err.Error())
	}

	log.Printf("gen %v robots into %v\n", *max, fileName)
}
